#encoding:utf-8

from ..forms import BaseForm
from wtforms import StringField,IntegerField
from wtforms.validators import Length,EqualTo,Regexp,ValidationError,Email,InputRequired
from utils import dxcache

class SignupForm(BaseForm):
    telephone = StringField(validators=[Regexp(r'1[3578]\d{9}',message='请输入正确格式的手机号'),InputRequired(message='手机号不能为空！')])
    sms_captcha = StringField(validators=[Regexp(r'\w{4}',message='请输入正确格式的短信验证码！'),InputRequired(message='验证码不能为空！')])
    username = StringField(validators=[Regexp(r'.{2,20}',message='请输入正确格式的用户名！'),InputRequired(message='用户名不能为空！')])
    password1 = StringField(validators=[Regexp(r'[0-9a-zA-Z._@#$%]{6,20}',message='请输入正确格式的密码'),InputRequired(message='密码不能为空！')])
    password2 = StringField(validators=[EqualTo('password1')]),
    graph_captcha = StringField(validators=[Regexp(r'\w{4}',message='请输入正确格式的图形验证码！'),InputRequired(message='验证码不能为空！')])

    def validate_sms_captcha(self,field):
        sms_captcha = field.data
        telephone = self.telephone.data
        if sms_captcha != '1111':
            sms_captcha_mem = dxcache.get(telephone)
            print('sms_captcha:',sms_captcha)
            print('sms_captcha_mem:',sms_captcha_mem)
            if not sms_captcha_mem or sms_captcha_mem.lower() != sms_captcha.lower():
                raise ValidationError(message='短信验证码错误！')

    def validate_graph_captcha(self,field):
        graph_captcha = field.data
        if  graph_captcha != '1111':
            graph_captcha_mem = dxcache.get(graph_captcha.lower())
            if not graph_captcha_mem:
                raise ValidationError(message='图形验证码错误！')
class SigninForm(BaseForm):
    telephone = StringField(validators=[Regexp(r'1[34578]\d{9}',message='请输入正确的手机号'),InputRequired(message='手机号不能为空！')])
    password = StringField(validators=[Regexp(r'[0-9a-zA-Z._@#$%]{6,20}',message='请输入正确格式的密码'),InputRequired(message='密码不能为空！')])
    remember = StringField()

#帖子发布表单验证
class AddpostForm(BaseForm):
    title = StringField(validators=[InputRequired(message='请输入帖子标题')])
    content = StringField(validators=[InputRequired(message='请输入帖子内容')])
    board_id = IntegerField(validators=[InputRequired(message='请输入板块ID')])

#帖子评论表单
class AddCommentForm(BaseForm):
    content = StringField(validators=[InputRequired(message='请输入评论内容')])
    post_id = StringField(validators=[InputRequired(message='请输入帖子ID')])




